﻿var Universe = function (sizeAndPosition, name, children) {

    $.extend(this, new UniverseEntity(sizeAndPosition, name, children));

    var _self = this;

    var viewDepth = 0;
    var viewDepthChangedEventHandlers = [];
    var shootingStarsOccuranceEventHandlers = [];
    var shootingStarsMaxValue = 3;

    this.ShootingStarsCount = new Range(0, shootingStarsMaxValue);
    this.ShootingStarsInterval = new Range(10000, 30000);
    var _nextShootingStarsInterval = this.ShootingStarsInterval.RandomInt();

    this.StarField = new StarField(this);
    this.AddChild(this.StarField);

    this.UniverseEntityType = UniverseEntityTypes.Universe;

    //Events
    this.AddViewDepthChangedHandler = function (handlerFuncRef) {
        this.RemoveViewDepthChangedHandler(handlerFuncRef);

        viewDepthChangedEventHandlers.push(handlerFuncRef);
    };

    this.AddShootingStarsOccuranceHandler = function (handlerFuncRef) {
        this.RemoveShootingStarsOccuranceHandler(handlerFuncRef);

        shootingStarsOccuranceEventHandlers.push(handlerFuncRef);
    };

    this.RemoveViewDepthChangedHandler = function (handlerFuncRef) {
        for (var index in viewDepthChangedEventHandlers) {
            if (viewDepthChangedEventHandlers[index] == handlerFuncRef) {
                viewDepthChangedEventHandlers.splice(index, 1);
            }
        }
    };

    this.RemoveShootingStarsOccuranceHandler = function (handlerFuncRef) {
        for (var index in shootingStarsOccuranceEventHandlers) {
            if (shootingStarsOccuranceEventHandlers[index] == handlerFuncRef) {
                shootingStarsOccuranceEventHandlers.splice(index, 1);
            }
        }
    };

    this.RaiseViewDepthChangedEvent = function () {
        for (var handlerIndex in viewDepthChangedEventHandlers) {
            viewDepthChangedEventHandlers[handlerIndex](this);
        }
    };

    this.RaiseShootingStarsOccuranceEvent = function () {
        for (var handlerIndex in shootingStarsOccuranceEventHandlers) {
            shootingStarsOccuranceEventHandlers[handlerIndex](this);
        }
    };

    this.ViewDepth = function (newDepth) {
        if (newDepth == null)
            return viewDepth;

        viewDepth = newDepth;
        this.RaiseViewDepthChangedEvent();
    };

    this.ToggleStarfield = function (isEnabled) {
        if (isEnabled)
            this.AddChild(this.StarField);
        else
            this.RemoveChild(this.StarField);
    };

    this.ToggleShootingStars = function (isEnabled) {
        if(isEnabled)
            this.ShootingStarsCount = new Range(0, shootingStarsMaxValue);
        else
            this.ShootingStarsCount = new Range(0, 0);
    };

    //Generate shooting star
    var ProcessShootingStars = function () {
        _self.RaiseShootingStarsOccuranceEvent();
        _nextShootingStarsInterval = _self.ShootingStarsInterval.RandomInt();
        setTimeout(ProcessShootingStars, _nextShootingStarsInterval);
    };
    setTimeout(ProcessShootingStars, _nextShootingStarsInterval);
};